package cn.alpha.intell.rbtlib.view.sysdialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import cn.alpha.intell.rbtlib.R;
import cn.alpha.intell.rbtlib.manager.image.main.IImageLoaderCallback;
import cn.alpha.intell.rbtlib.manager.image.main.ImageLoaderFactory;
import cn.alpha.intell.rbtlib.rbt.utils.RBTKeyUtil;
import cn.alpha.intell.rbtlib.share.CircleImageView;



/**
 * @author white
 * @description：系统弹窗统一baseDialog
 * @date 2016/10/10
 */

public abstract class SystemBaseDialog extends Dialog {

    protected ImageView mBtnClose, mBtnOk;
    protected TextView mTvTitle, mTvContent;
    protected ImageView mIvLogo;
    protected CircleImageView mCircleImageView;
    protected Context mContext;
    protected OnDismissListener mDismissListener;

    protected boolean mIsCancel;
    protected String mTitle;
    protected String mContent;
    protected Uri mUri;

    public void setCallBack(CallBack callBack) {
        mCallBack = callBack;
    }

    private CallBack mCallBack;

    public SystemBaseDialog(Context context) {
        super(context, R.style.RbtLib_Dialog_Fullscreen);
        this.mContext = context;
        this.mTitle = getContentText();
        this.mContent = getTipsText();
        initView(context);
    }

    public SystemBaseDialog(Context context, int resid) {
        super(context, resid);
        this.mContext = context;
        initView(context);
    }

    public SystemBaseDialog(Context context, String title, String content) {
        this(context, title, content, true);
    }

    public SystemBaseDialog(Context context, String title, String content, boolean isCancel) {
        this(context, title, content, null, isCancel);
    }

    public SystemBaseDialog(Context context, String title, String content, Uri uri, boolean isCancel) {
        super(context, R.style.RbtLib_Dialog_Fullscreen);
        this.mContext = context;
        this.mTitle = title;
        this.mContent = content;
        this.mIsCancel = isCancel;
        this.mUri = uri;
        initView(context);
    }


    protected void initView(Context context) {

        LayoutInflater inflater = LayoutInflater.from(context);
        View layout = inflater.inflate(getContentViewId(), null);
//        setContentView(getContentViewId());
        setContentView(layout);
        getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
//        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
//                            WindowManager.LayoutParams.FLAG_FULLSCREEN);

        mBtnClose = (ImageView) findViewById(R.id.btn_close);
        mBtnOk = (ImageButton) findViewById(R.id.btn_ok);
        mTvTitle = (TextView) findViewById(R.id.tv_title);
        mTvContent = (TextView) findViewById(R.id.tv_content);
        mIvLogo = (ImageView) findViewById(R.id.iv_logo);
        mCircleImageView = (CircleImageView) findViewById(R.id.iv_logo_circel);

        initParams();
    }


    public void setDialogType(int windowType){
        getWindow().setType(windowType);
    }

    /**
     * 关闭弹窗,恢复home和voice键
     * @param listener
     */
    @Override
    public void setOnDismissListener(OnDismissListener listener) {
        mDismissListener = listener;
        super.setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialog) {
                RBTKeyUtil.enableKeyHome(true);
                RBTKeyUtil.enableKeyVoice(true);
                if (mDismissListener != null) {
                    mDismissListener.onDismiss(dialog);
                }
            }
        });
    }

    @Override
    public void show() {
        RBTKeyUtil.enableKeyHome(false);
        RBTKeyUtil.enableKeyVoice(false);
        super.show();
    }

    protected void initParams() {
        RBTKeyUtil.enableKeyHome(false);
        RBTKeyUtil.enableKeyVoice(false);

        this.setCanceledOnTouchOutside(false);
        this.setCancelable(mIsCancel);
        setOnDismissListener(null);

        mBtnClose.setVisibility(this.mIsCancel ? View.VISIBLE : View.GONE);
        mBtnClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(mCallBack!=null){
                    mCallBack.doCanCel();
                }else {
                    onClickCloseBtn();
                }
                dismiss();
            }
        });
        mBtnOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mCallBack!=null){
                    mCallBack.doOk();
                }else {
                    onClickOkBtn();
                }
            }
        });

        mIvLogo.setImageDrawable(getLogoImageResId());
        mBtnOk.setBackgroundDrawable(getOkBtnBgResId());
        mBtnOk.setImageDrawable(getOkBtnSrcResId());

        mTvTitle.setText(this.mTitle);
        if (TextUtils.isEmpty(this.mContent)) {
            mTvContent.setVisibility(View.GONE);
        } else {
            mTvContent.setVisibility(View.VISIBLE);
            mTvContent.setText(this.mContent);
        }
        if(this.mUri!=null){
            mCircleImageView.setVisibility(View.VISIBLE);
            mIvLogo.setVisibility(View.GONE);
            ImageLoaderFactory
                    .getInstance()
                    .loadImage(mCircleImageView, mUri.toString(), new IImageLoaderCallback() {
                        @Override
                        public void onFinish(Bitmap bitmap) {
                            mCircleImageView.setImageBitmap(bitmap);
                        }
                    });
        }else {

            mCircleImageView.setVisibility(View.GONE);
            mIvLogo.setVisibility(View.VISIBLE);
        }


    }

    protected int getContentViewId() {
        return R.layout.dialog_system_base;
    }

    protected void onClickCloseBtn() {
        if (isShowing()) {
            dismiss();
        }
    }


    /**
     * 点击OK事件处理
     */
    protected abstract void onClickOkBtn();

    /**
     * 获取Ok按钮bg 资源id
     * @return
     */
    protected abstract Drawable getOkBtnBgResId();

    /**
     * 获取Ok按钮src 资源id
     * @return
     */
    protected abstract Drawable getOkBtnSrcResId();

    /**
     * 获取login src 资源id
     * @return
     */
    protected abstract Drawable getLogoImageResId();

    /**
     * 获取内容
     * @return
     */
    protected abstract String getContentText();

    /**
     * 获取提示内容
     * @return
     */
    protected abstract String getTipsText();

    public interface CallBack{

        void doOk();
        void doCanCel();
    }

}
